//
//  CircleListMainView.m
//  RACExample
//
//  Created by 山刘 on 2020/6/3.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "CircleListMainView.h"
#import "CircleListMainViewCell.h"

@interface CircleListMainView()<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *mainTableView;
@end

@implementation CircleListMainView
- (instancetype)initWithViewModel:(CircleListMainViewModel*)viewModel {
    if (self == [super init]) {
        self.backgroundColor = self.superview.backgroundColor;
        _viewModel = viewModel;
        [self segInitViews];
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        [self bindViewModel];
    }
    return self;
}

#pragma mark - init Views
-(void)segInitViews{
    [self addSubview:self.mainTableView];
}

#pragma mark - Layout
- (void)updateConstraints {
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [super updateConstraints];
}
#pragma mark - RAC Data Binding
- (void)bindViewModel {
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    
    // 数据请求
//    NSLog(@"refreshDataCommand execute");
    [self.viewModel.refreshDataCommand execute:nil];
    // 数据刷新
    [self.viewModel.refreshDataSubject subscribeNext:^(NSDictionary*  _Nullable x) {
        [self.mainTableView.mj_header endRefreshing];
        [self.mainTableView.mj_footer endRefreshing];
        
        [self.mainTableView reloadData];
        
        NSInteger code = [[x valueForKey:@"code"] integerValue];
        NSString* msg = [x valueForKey:@"msg"];
        
        switch (code) {
            case RefreshLoading:
//                NSLog(@"RefreshLoading");
                [SVProgressHUD show];
                break;
            case RefreshError:
//                NSLog(@"RefreshError");
                [SVProgressHUD showErrorWithStatus:msg];
                break;
            case RefreshUI:
//                NSLog(@"RefreshUI");
                [SVProgressHUD showSuccessWithStatus:msg];
                break;
            default:
                break;
        }
    }];
}

#pragma mark - getter
-(CircleListMainViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[CircleListMainViewModel alloc]init];
    }
    return _viewModel;
}
- (UITableView *)mainTableView {
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] init];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.backgroundColor = self.backgroundColor;
//        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.viewModel.refreshDataCommand execute:nil];
        }];
        _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self.viewModel.refreshDataCommand execute:nil];
        }];
    }
    return _mainTableView;
}

#pragma mark - ====================delegate====================

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CircleListMainViewCell *cell = [CircleListMainViewCell cellWithTableView:tableView indexPath:indexPath];
    if (self.viewModel.dataArray.count > indexPath.row) {
        cell.viewModel = self.viewModel.dataArray[indexPath.row];
    }
    [cell.viewModel.buttonClickSubject subscribeNext:^(id  _Nullable x) {
        NSLog(@"raiseButton subscribeNext--%@",x);
    }];
//    [[cell.raiseButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        NSLog(@"raiseButton subscribeNext");
//    }];
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.viewModel.dataArray.count > indexPath.row) {
        [self.viewModel.cellClickSubject sendNext:self.viewModel.dataArray[indexPath.row]];
    }
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//
//    return self.sectionHeaderView;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//
//    return 45;
//}
@end
